/*--------------------------------------------------------------------------
    This source file is part of Abraxas Game Engine
    For the latest info, see http://code.google.com/abraxasge/
    Copyright (C) 2009, Andrew LeRoy <aleroy@member.igda.org>
    ---

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA  02110-1301, USA.
--------------------------------------------------------------------------*/
#include "AbxException.hpp"
#include "AbxEngine.hpp"
#include "AbxLogManager.hpp"


#ifdef __BORLANDC__
    #include <stdio.h>
#endif

namespace Abx
{
    Exception::Exception(int num, String const& desc, String const& src) :
        line(0),
        number(num),
        description(desc),
        source(src)
    {
        // Log this error - not any more, allow catchers to do it
        //LogManager::getSingleton().logMessage(this->getFullDescription());
    }

    Exception::Exception(int num, String const& desc, String const& src, 
        const char* typ, const char* fil, long lin) :
        line(lin),
        number(num),
        typeName(typ),
        description(desc),
        source(src),
        file(fil)
    {
        // Log this error, mask it from debug though since it may be caught and ignored
        LogManager::Instance().logMessage(this->getFullDescription(), LOG_ENTRY_ERROR);

    }

    Exception::Exception(Exception const& rhs)
        : line(rhs.line), 
        number(rhs.number), 
        typeName(rhs.typeName), 
        description(rhs.description), 
        source(rhs.source), 
        file(rhs.file)
    {
    }

    void Exception::operator = (Exception const& rhs)
    {
        description = rhs.description;
        number = rhs.number;
        source = rhs.source;
        file = rhs.file;
        line = rhs.line;
        typeName = rhs.typeName;
    }

    String const& Exception::getFullDescription(void) const
    {
        if (fullDesc.empty())
        {
            StringTools::StrStreamType desc;

            desc << "AbraxasGE EXCEPTION(" << number << ":" << typeName << "): "
                 << description 
                 << " in " << source;

            if( line > 0 )
            {
                desc << " at " << file << " (line " << line << ")";
            }

            fullDesc = desc.str();
        }

        return fullDesc;
    }

    int Exception::getNumber(void) const throw()
    {
        return number;
    }
    
} // namespace Abx
